package com.example.rabiitmq.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-04-02
 * Time: 8:49
 */
@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/work")
    public String work() {
        for (int i = 0; i < 10; i++) {
            //使用内置交换机，RoutingKey和队列名相同
            rabbitTemplate.convertAndSend("","queue1","hello spring ampq work...");
        }
        return "发送成功";
    }


    @RequestMapping("/fanout")
    public String fanout() {
        rabbitTemplate.convertAndSend("fanoutExchange","","hello spring fanout ampq...");
        return "发送成功";
    }

    @RequestMapping("/direct/{routingKey}")
    public String direct(@PathVariable("routingKey") String routingKey) {
        rabbitTemplate.convertAndSend("directExchange",routingKey,"hello spring direct amqp + " + routingKey);
        return "发送成功";
    }

    @RequestMapping("/topic/{routingKey}")
    public String topic(@PathVariable("routingKey") String routingKey) {
        rabbitTemplate.convertAndSend("topicExchange",routingKey,"hello spring topic amqp + " + routingKey);
        return "发送成功";
    }
}
